package cn.redragon.soa.common.util;

/**
 * The Class CryptException.
 */
@SuppressWarnings("serial")
public class CryptException extends Exception {

    /**
     * The Constant GENERATE_AES_KEY_ERROR.
     */
    public final static int GENERATE_AES_KEY_ERROR = -40201;

    /**
     * The Constant GENERATE_SIGNATURE_ERROR.
     */
    public final static int GENERATE_SIGNATURE_ERROR = -40202;

    /**
     * The Constant ENCRYPT_AES_ERROR.
     */
    public final static int ENCRYPT_AES_ERROR = -40203;

    /**
     * The Constant DECRYPT_AES_ERROR.
     */
    public final static int DECRYPT_AES_ERROR = -40204;

    /**
     * The Constant DECRYPT_USER_INFO_ERROR.
     */
    public final static int DECRYPT_USER_INFO_ERROR = -40205;

    /**
     * The Constant ENCRYPT_SHA1_ERROR.
     */
    public final static int ENCRYPT_SHA1_ERROR = -40206;

    /**
     * The code.
     */
    private int code;

    /**
     * Instantiates a new crypt exception.
     *
     * @param code the code
     */
    public CryptException(int code) {
        super(getMessage(code));
        this.code = code;
    }

    /**
     * Instantiates a new crypt exception.
     *
     * @param code  the code
     * @param cause the cause
     */
    public CryptException(int code, Throwable cause) {
        super(getMessage(code), cause);
        this.code = code;
    }

    /**
     * Gets the message.
     *
     * @param code the code
     * @return the message
     */
    private static String getMessage(int code) {
        switch (code) {
            case GENERATE_AES_KEY_ERROR:
                return "Generate AES key failed.";
            case GENERATE_SIGNATURE_ERROR:
                return "Generate sha singature failed.";
            case ENCRYPT_AES_ERROR:
                return "Encrypt with AES failed.";
            case DECRYPT_AES_ERROR:
                return "Decrypt with AES failed.";
            case DECRYPT_USER_INFO_ERROR:
                return "Decrypt user info failed.";
            case ENCRYPT_SHA1_ERROR:
                return "Encrypt with SHA1 failed.";
            default:
                return null;
        }
    }

    /**
     * Gets the code.
     *
     * @return the code
     */
    public int getCode() {
        return code;
    }
}
